<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\order\model;

/**
 * Description of Order
 * 充值订单
 * @author zmh
 */
class OrderCms extends \web\common\model\BaseModel{
    
    protected $rows = 10;
    protected function _initialize(){
        $this->tableName = 'recharge_order_cms';
    }

    /**
     * 获取记录总数
     * @param type $filter
     * @return int
     */
    public function getTotal($filter = '') {
        $sql = 'select count(o.id) as c from '.$this->getTableName() .' o left join tp_member_account m on m.id=o.user_id';
        if (!empty($filter)){
            $sql = $sql . ' where 1=1 ' . $filter;
        }
        $result = $this->query($sql);
        if (count($result) > 0)
            return intval($result[0]['c']);
        else
            return 0;
    }


    public function getDataList($pageIndex = -1, $pageSize = -1, $filter = '',$fileds='*', $order = 'id desc') {
        $sql = 'select o.*,m.username from '.$this->getTableName() . ' o left join tp_member_account m on m.id=o.user_id where 1=1 '.$filter;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

    /**
     * 回调成功
     */
    public function paySuccess($data) {
        $map['ref_no'] = $data['out_trade_no'];
        $info = $this->where($map)->find();
        if($info['status']!=0){
            return true;
        }
        $update = [
                'pay_amount' => $data['total_fee']/100,
                'pay_time' => date('Y-m-d H:i:s',strtotime($data['time_end'])),
                'status' => 10,
                'pay_type' => $data['pay_type'],
        ];
        $res = $this->where($map)->update($update);
        if($res){
            return $info;
        }else{
            return false;
        }
    }

    /**
     * 返回成功数据。
     * @param type $data
     * @return type
     */
    protected function successData($data = null) {
        return array('success' => true, 'data' => $data);
    }

    /**
     * 返回失败数据。
     * @param type $message
     * @return type
     */
    protected function failData($message = '') {
        return array('success' => false, 'message' => $message);
    }
    
}
